local skill = fk.CreateSkill{
  name = "tea__xiluan",
  tags = { Skill.Compulsory }
}
---@type TrigSkelSpec<TrigFunc>
local xiluan = {
  anim_type = "big",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skill.name) and player == target and player.phase == Player.Finish and player:usedSkillTimes(skill.name, Player.HistoryGame) == 0 then
      local list = {}
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              if player.room:getCardArea(Fk:getCardById(info.cardId)) == Card.DiscardPile and Fk:getCardById(info.cardId).is_damage_card then
                table.insertIfNeed(list, info.cardId)
              end
            end
          end
        end
        return false
      end, Player.HistoryTurn)
      if #list > 0 then
        event:setCostData(self, list)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self)
    local suc, dat = room:askToUseActiveSkill(player, {
      skill_name = "tea__xiluan_choose_skill",
      prompt = "#tea__xiluan-invoke",
      cancelable = false,
      extra_data = {
        expand_pile = cards,
      }
    })
    if suc and dat then
      if #dat.targets == 1 and #dat.cards > 0 then
        local to = dat.targets[1]
        to:addToPile("#tea__xiluan_pile", dat.cards, true, skill.name, nil, table.map(room.alive_players, Util.IdMapper))
        room:setPlayerMark(to, "@$tea__xiluan_mark", table.map(dat.cards, function(id)
          return Fk:getCardById(id).name
        end))
      end
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local xiluan_delay = {
  can_refresh = function(self, event, target, player, data)
    if player == target and #player:getPile("#tea__xiluan_pile") > 0 then
      return table.find(player:getPile("#tea__xiluan_pile"), function(id)
        return Fk:getCardById(id).trueName == data.card.trueName
      end)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if player.room:getTag("RoundCount") < 3 then
      player.room:notifySkillInvoked(player, skill.name, "defensive")
      data.damage = data.damage - 1
    else
      player.room:notifySkillInvoked(player, skill.name, "negative")
      data.damage = data.damage + 1
    end
  end,
}
skill:addEffect(fk.EventPhaseStart, xiluan)
skill:addEffect(fk.DamageInflicted, xiluan_delay)
return skill